//
//  AppDelegate.m
//  mendianjieke
//
//  Created by 响当当 on 7/30/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "AppDelegate.h"
#import "NewLoginViewController.h"
#import "SCNewFeatureController.h"

@interface AppDelegate ()<UITabBarControllerDelegate>
@end

@implementation AppDelegate


- (HWTabBarController *)vc
{
    if (!_vc)
    {
        _vc = [[HWTabBarController alloc]init];
        _vc.delegate = self;
    }
    
    return _vc;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    NSString *str = LCUserDefaultsGet(@"bageNum");
    [self showTabCount:str];
    
    NSString *voice = IFNULL(LCUserDefaultsGet(@"Voice"), @"-1");
    NSString *shake = IFNULL(LCUserDefaultsGet(@"Shake"), @"-1");
    if (voice.integerValue == -1)
    {
        LCUserDefaultsSet(@"1", @"Voice");
        LCUserDefaultsSyn;
    }
    
    if (shake.integerValue == -1)
    {
        LCUserDefaultsSet(@"1", @"Shake");
        LCUserDefaultsSyn;
    }

    
    [APNsManager resetAPNsPushWithLaunchOptions:launchOptions];
    [self resetFMDB];
    [self resetSVP];
    //监控网络状态
    [Utilties monitorNetworking];
    
    if ([Utilties isFirstTimeStartover])
    {
        SCNewFeatureController *newFeatureVC = [[SCNewFeatureController alloc] init];
        // 设置窗口的根控制器
        self.window.rootViewController = newFeatureVC;
    }
    else
    {
        if (![current_User.hasLogin isValidString] || [current_User.hasLogin isEqualToString:@"0"])
        {
            NewLoginViewController *vc = [[NewLoginViewController alloc]init];
            self.window.rootViewController = vc;
        }
        else
        {
            self.window.rootViewController = self.vc;
        }
        
        [self.window makeKeyAndVisible];
    }

    
    return YES;
}

#pragma 初始化SVP
- (void)resetSVP
{
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
}

#pragma 初始化fmdb
- (void)resetFMDB
{
    [LCFMDB shareDatabase:@"MDDH_User"];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
   if ([tabBarController.viewControllers containsObject:tabBarController.selectedViewController])
   {
        if ([viewController.tabBarItem.title isEqualToString:@"实时"])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DOUBLEREFRESH" object:nil userInfo:nil];
            return YES;
        }
    }
    return YES;
    
}

- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
    if (![deviceToken isKindOfClass:[NSData class]]) return;
    const unsigned *tokenBytes = (const unsigned *)[deviceToken bytes];
    NSString *hexToken = [NSString stringWithFormat:@"%08x%08x%08x%08x%08x%08x%08x%08x",
                          ntohl(tokenBytes[0]), ntohl(tokenBytes[1]), ntohl(tokenBytes[2]),
                          ntohl(tokenBytes[3]), ntohl(tokenBytes[4]), ntohl(tokenBytes[5]),
                          ntohl(tokenBytes[6]), ntohl(tokenBytes[7])];
    LCUserDefaultsSet(hexToken, @"device_token");
    LCUserDefaultsSyn;
}

- (BOOL)application:(UIApplication*)application shouldAllowExtensionPointIdentifier:(NSString*)extensionPointIdentifier

{

    return NO;

}

- (void)gotoHome
{
    self.vc.selectedViewController = [self.vc.viewControllers objectAtIndex:0];
}

- (void)gotoMeCenter
{
    self.vc.selectedViewController = [self.vc.viewControllers objectAtIndex:3];
}


- (void)showTabCount:(NSString *)str
{
    UITabBarItem * item=[self.vc.tabBar.items objectAtIndex:1];
    // 显示
    if (str.integerValue == 0)
    {
        item.badgeValue=nil;
    }
    else
    {
        item.badgeValue=str;
    }
}

- (void)gotoHomeWithtabBarVC:(HWTabBarController *)vc
{
    vc.selectedViewController = [vc.viewControllers objectAtIndex:0];
}

- (void)showTabCount:(NSString *)str tabBarVC:(HWTabBarController *)vc
{
    UITabBarItem * item=[vc.tabBar.items objectAtIndex:1];
    // 显示
    if (str.integerValue == 0)
    {
        item.badgeValue=nil;
    }
    else
    {
        item.badgeValue=str;
    }
}




@end
